<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading
      v-if='loading'
      class="article-loading"
    />
    <!-- /加载中 loading -->

    <!-- 404页面 -->
    <div
      class="error"
      v-if="is404"
    >
      <p>文章被外星人吃掉了</p>
      <van-button @click="$router.back()">后退</van-button>
      <van-button @click="$router.push('/')">回主页</van-button>
    </div>

    <!-- 文章详情 -->
    <div
      class="detail"
      v-else
    >
      <h3 class="title">{{article.title}}</h3>
      <author :article='article'>
        <van-button
          round
          size="small"
          type="info"
          @click="hSwitchFollowed"
        >{{isFollowed ? '已关注': '+ 关注'}}</van-button>
      </author>
      <!-- 作者组件封装 -->
      <!-- <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | relativeTime}}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="hSwitchFollowed"
        >{{isFollowed ? '已关注': '+ 关注'}}</van-button>
      </div> -->
      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="isLike===1? 'good-job':'good-job-o'"
          @click="hSwitchLike"
        >{{isLike === 1?'取消点赞':'点赞'}}</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          @click="hSwitchUnlike"
        >{{isUnlike===0?'取消不喜欢':'不喜欢'}}</van-button>
      </div>
      <article-comment :articleID="$route.params.articleID">
      </article-comment>
    </div>
    <!-- /文章详情 -->

  </div>
</template>

<script>
import author from '@/components/author.vue'
import ArticleComment from './comment.vue'
import { getArticleDetail, followedAuthor, unfollowedAuthor, giveLike, cancleLike, unlikeArticles, cancleUnlike } from '@/api/articles.js'
export default {
  name: 'ArticleIndex',
  data () {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: {},
      isFollowed: false, // 作者是否被关注
      isLike: null, // 文章是否被点赞
      isUnlike: null, // 当前文章是否不喜欢
      is404: false
    }
  },
  components: {
    author,
    ArticleComment
  },
  created () {
    // 3、页面点开时调用获取新闻信息
    this.loadArticleDetail()
  },
  methods: {
    // 获取文章详情
    async loadArticleDetail () {
      try {
        // 1、获取路径参数
        const articleID = this.$route.params.articleID
        // 2、发送请求获取内容详情
        const res = await getArticleDetail(articleID)
        // console.log(res)
        // 4、将返回结果赋值给artice驱动视图更新
        this.article = res.data.data
        // console.log(this.article)
        // 确认当前作者是否被用户关注
        this.isFollowed = this.article.is_followed
        // 确认当前文章是否被点赞
        this.isLike = this.article.attitude
        // console.log(this.isLike)
        // 确认当前文章是否不被喜欢
        this.isUnlike = this.article.attitude
        // 5、关闭加载
        this.loading = false
      } catch (err) {
        console.log(err)
        if (err.response.status === 404) {
          this.is404 = true
          this.loading = false
        }
      }
    },
    // 是否关注操作
    async hSwitchFollowed () {
      // 如果当前作者被关注，则点击时调用取消关注接口，如果当前未关注，则调用关注接口
      if (this.isFollowed) {
        // 1、发送请求
        const res = await unfollowedAuthor(this.article.aut_id)
        console.log(res)
        // 2、更改isFollowed设置
        this.isFollowed = false
      } else {
        // 1、发送请求
        const res = await followedAuthor(this.article.aut_id)
        console.log(res)
        // 2、更改isFollowed设置
        this.isFollowed = true
      }
    },
    // 切换点赞和取消点赞
    async hSwitchLike () {
      // console.log(this.isLike)
      if (this.isLike === 1) {
        const res = await cancleLike(this.article.art_id.toString())
        console.log(res)
        this.isLike = -1
      } else {
        const res = await giveLike(this.article.art_id.toString())
        console.log(res)
        this.isLike = 1
      }
    },
    // 点击时切换不喜欢状态
    async hSwitchUnlike () {
      if (this.isUnlike === 0) {
        const res = await cancleUnlike(this.article.art_id.toString())
        console.log(res)
        this.isUnlike = -1
      } else {
        const res = await unlikeArticles(this.article.art_id.toString())
        console.log(res)
        this.$toast.success('以后会减少此类文章推荐')
        this.isUnlike = 0
      }
    }
  }
}
</script>

<style scoped lang='less'>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
  /deep/.van-nav-bar .van-icon {
    color: #fff;
  }
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
